package com.numberone.master.modules.recharge.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.card.entity.Card;
import com.numberone.master.modules.card.service.CardService;
import com.numberone.master.modules.recharge.entity.RechargeLs;
import com.numberone.master.modules.recharge.service.RechargeLsService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * 流量卡充值Controller
 * @author chengkang
 * @version 2018-02-09
 */
@Controller
@RequestMapping(value = "${adminPath}/recharge/rechargeLs")
public class RechargeLsController extends BaseController {

	@Autowired
	private RechargeLsService rechargeLsService;
	
	@Autowired
	private CardService cardService;

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public RechargeLs get(@RequestParam(required=false) String id) {
		RechargeLs entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = rechargeLsService.get(id);
		}
		if (entity == null){
			entity = new RechargeLs();
		}
		return entity;
	}

	 /**
     * list列表
     * @param rechargeLs
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("recharge:rechargeLs:list")
	@RequestMapping(value = {"list", ""})
	public String list(RechargeLs rechargeLs, HttpServletRequest request, HttpServletResponse response, Model model) {
		String id = UserUtils.getUser().getOffice().getId();
		rechargeLs.setCzstate("0,1");
		if(!StringUtils.equals(id, "1")){
			rechargeLs.setAgentId(id);
		}
		Page<RechargeLs> page = rechargeLsService.findPage(new Page<RechargeLs>(request, response), rechargeLs); 
		model.addAttribute("page", page);
		return "modules/recharge/rechargeFlowList";
	}
	
	/**
	 * 物联卡充值审核列表
	 * @param rechargeLs
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:examine:list")
	@RequestMapping(value = {"examine_list", ""})
	public String examineList(RechargeLs rechargeLs, HttpServletRequest request, HttpServletResponse response, Model model) {
		rechargeLs.setPaystate("0");
		rechargeLs.setCzstate("1");
		Page<RechargeLs> page = rechargeLsService.findPage(new Page<RechargeLs>(request, response), rechargeLs); 
		model.addAttribute("page", page);
		return "modules/recharge/examineList";
	}
	
	/**
	 * 审核操作
	 * @param ids
	 * @param money
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:examine:examine")
	@RequestMapping(value = "examine")
	public String examine(String[] ids,RedirectAttributes redirectAttributes) {
			int num = 0;
			for (int i = 0; i < ids.length; i++) {
				RechargeLs rechargeLs = new RechargeLs();
				rechargeLs.setId(ids[i]);
				rechargeLs.setCzstate("0");//修改状态
				rechargeLs.setUpdateDate(new Date());
				rechargeLsService.examine(rechargeLs);
				//添加分润信息
				
				num++;
			}
			addMessage(redirectAttributes, "成功审核"+num+"张流量卡");
			return "redirect:"+adminPath+"/recharge/rechargeLs/examine_list?repage";
	}
	
	/**
	 * 流量卡列表
	 * @param card
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:rechargeLs:rechargeLsList")
	@RequestMapping(value = {"rechargeLs_list", ""})
	public String rechargeLsList(Card card, HttpServletRequest request, HttpServletResponse response, Model model) {
		String officeId = UserUtils.getUser().getOffice().getId();
		if(!"1".equals(officeId)){
			card.setOfficeId(officeId);
		}
		card.setCardStatus(new Long(1));
		Page<Card> page = cardService.findPage(new Page<Card>(request, response), card); 
		model.addAttribute("page", page);
		return "modules/recharge/rechargeCardList";
	}
	
	/**
	 * 充值操作
	 * @param ids
	 * @param officeId
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:rechargeLs:recharge")
	@RequestMapping(value = "recharge")
	public String recharge(String[] ids,String money,RedirectAttributes redirectAttributes) {
			int num = 0;
			for (int i = 0; i < ids.length; i++) {
				//rechargeLsService.recharge(ids[i],money);
				num++;
			}
			addMessage(redirectAttributes, "成功充值"+num+"张流量卡");
			return "redirect:"+adminPath+"/recharge/rechargeLs/rechargeLs_list?repage";
	}

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("recharge:rechargeLs:save")
	@RequestMapping(value = "openSave")
	public String openSave() {
		return "modules/recharge/rechargeLsSave";
	}


	/**
	 * 打开修改页面
	 * @param rechargeLs
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:rechargeLs:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(RechargeLs rechargeLs, Model model) {
		model.addAttribute("rechargeLs", rechargeLs);
		return "modules/recharge/rechargeLsUpdate";
	}

	/**
	 * 添加
	 * @param rechargeLs
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:rechargeLs:save")
	@RequestMapping(value = "save")
	public String save(RechargeLs rechargeLs, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rechargeLs)){
			return openSave();
		}
		rechargeLsService.save(rechargeLs);
		addMessage(redirectAttributes, "添加流量卡充值成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/rechargeLs/list?repage";
	}

	/**
	 * 修改
	 * @param rechargeLs
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:rechargeLs:update")
	@RequestMapping(value = "update")
	public String update(RechargeLs rechargeLs, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rechargeLs)){
			return openUpdate(rechargeLs, model);
		}
		rechargeLsService.update(rechargeLs);
		addMessage(redirectAttributes, "修改流量卡充值成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/rechargeLs/?repage";
	}

	 /**
     * 删除
     * @param rechargeLs
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("recharge:rechargeLs:delete")
	@RequestMapping(value = "delete")
	public String delete(RechargeLs rechargeLs, RedirectAttributes redirectAttributes) {
		rechargeLsService.delete(rechargeLs);
		addMessage(redirectAttributes, "删除流量卡充值成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/rechargeLs/?repage";
	}

}